from django.shortcuts import render, HttpResponse
from django.views.generic import View
from app03.models import *
from django.http.response import JsonResponse


# Create your views here.
class AddView(View):
    def get(self, request):
        # 操作对象  需要模型
        # insert into app03_host values(null,xxx,xxx,xxx,xxx)
        # 方法一:
        # host = Host(hostname='server01',ip='192.168.19.100',type=1,status=0)
        # host.save()
        # 方法二：
        # host = Host()
        # host.hostname = 'server02'
        # host.ip = '192.168.19.101'
        # host.type = 2
        # host.status = 0
        # host.save()
        # 方法三:
        # Host.objects.create(hostname='server03',ip='192.168.19.102',type=1,status=1)
        # 方法四:
        data = {'hostname': 'server04', 'ip': '192.168.19.103', 'type': 4, 'status': 1}
        # **data  **代表传入字典数据
        Host.objects.create(**data)
        return HttpResponse('添加数据')


class ShowView(View):
    def get(self, request):
        # all方法 查询出所有的数据
        # data = Host.objects.all()
        # get 方法 查询出一条信息
        # get查询只返回一条数据，如果有没有或者有多条 就会返回一个异常
        # data = Host.objects.get(hostname='server01')
        # filter方法 一条或者多条数据  不管查询一个还是多个 返回的都是一个queryset
        # data = Host.objects.filter(status=0)
        # print(data[0:1])
        # exclude方法   除了xx条件之外的数据
        # status 0 在线  1 不在线
        # data = Host.objects.exclude(status=0)
        # values方法  筛选显示数据的字段信息
        # data = Host.objects.filter(status=1).values('hostname','ip')
        # __方法
        # data = Host.objects.filter(status__gt=0)
        # data = Host.objects.filter(hostname__contains='server')
        data = Host.objects.filter(hostname__regex='^server')
        print(data)
        return HttpResponse('查询数据')


class UpdateView(View):
    def get(self, request):
        # 修改数据字段值信息
        # 方法一: 对象设置属性方式
        # host = Host.objects.get(hostname='server01')
        # host.hostname = 'Server01'
        # host.save()
        # 方法二: 通过update方法
        Host.objects.filter(status=1).update(status=0)
        return HttpResponse('更新数据')


class DeleteView(View):
    def get(self, request):
        # delete方法
        Host.objects.filter(hostname='server04').delete()
        return HttpResponse('删除数据')


class StudentView(View):
    def get(self, request):
        # 添加数据
        # stu = Student1(name='张三')
        # stu.save()
        # 查询数据
        # data = Student1.objects.all()
        # print(data)
        # 修改数据
        # stu = Student1.objects.get(id=1)
        # stu.name = 'zhangsan'
        # stu.save()
        # 删除数据
        Student1.objects.get(id=1).delete()
        return HttpResponse('学生管理')


class HostListView(View):
    def get(self, request):
        hostlist = Host.objects.all()
        return render(request, 'hostlist.html', locals())
        # return HttpResponse('app03的主机列表方法')

    # 表单接收添加数据
    # def post(self,request):
    #     print(request.POST)
    #     data = request.POST
    #     try:
    #         host = Host(hostname=data.get('hostname'),ip=data.get('ip'),type=data.get('type'),status=data.get('status'))
    #         host.save()
    #     except Exception as e:
    #         print(e)
    #         return HttpResponse("<script>document.write('<h1>添加失败,3s后跳转</h1>');setTimeout( \"window.location.href='http://127.0.0.1:8000/app03/hostlist'\",3000)</script>")
    #     return HttpResponse("<script>document.write('<h1>添加成功,3s后跳转</h1>');setTimeout( \"window.location.href='http://127.0.0.1:8000/app03/hostlist'\",3000)</script>")
    def post(self, request):
        print(request.POST)
        data = {}
        for one in request.POST.items():
            if one[0] != 'csrfmiddlewaretoken':
                data[one[0]] = one[1]
        # print(data)
        try:
            Host.objects.create(**data)
        except Exception as e:
            print(e)
            return JsonResponse({'status': 1, 'msg': '添加失败'})
        return JsonResponse({'status': 0, 'msg': '添加成功'})


class HostStatusView(View):
    def get(self, request):
        # print(request.GET)
        host = Host.objects.get(id=request.GET.get('id'))
        current_status = host.status
        # 如果目前是启用状态,就修改禁用
        if current_status == 0:
            status = 1
        else:
            # 如果目前是禁用状态，就修改启用
            status = 0
        try:
            host.status = status
            host.save()
        except Exception as e:
            print(e)
            return JsonResponse({'status': 1, 'msg': '状态修改失败'})
        return JsonResponse({'status': 0, 'msg': '状态修改成功'})


class HostDeleteView(View):
    def get(self, request):
        try:
            Host.objects.get(id=request.GET.get('id')).delete()
        except Exception as e:
            print(e)
            return JsonResponse({'status': 1, 'msg': '删除失败'})
        return JsonResponse({'status': 0, 'msg': '删除成功'})


class OneToManyView(View):
    def get(self, request):
        # 添加数据
        # 先添加出版社信息
        # Publish.objects.create(name='清华大学出版社',address='北京')
        # Publish.objects.create(name='东北大学出版社',address='沈阳')
        # Publish.objects.create(name='东北财经大学出版社',address='大连')
        # 再添加图书信息 关联关系
        # Book.objects.create(name='Linux高级运维详解',price='89.60',publish=Publish.objects.get(name='清华大学出版社'))
        # Book.objects.create(name='DevOps设计理念',price='60.50',publish=Publish.objects.get(name='东北大学出版社'))
        # Book.objects.create(name='Python高级开发',price='50.43',publish=Publish.objects.get(name='清华大学出版社'))
        # 查询
        # 查询图书信息和出版社信息
        # data = Book.objects.all()
        # print(data)
        # for one in data:
        # 取出图书信息和出版社信息
        # 查询出版社信息 正向查询
        # print(one.name,one.price,one.publish.name)
        # 查询出版社有哪些图书 反向查询
        # data = Publish.objects.get(name='清华大学出版社').book_set.all()
        # for one in data:
        #     print(one.name)
        # 删除
        # 判断如果出版社下面有图书,就不允许删除出版社
        publish = Publish.objects.get(name='东北大学出版社')
        if publish.book_set.all():
            print('出版社下面有图书,不允许删除')
        else:
            publish.delete()
        return HttpResponse('一对多关系学习测试')
